﻿
namespace Sai.Core.Introspection
{
    public static class StringExtensions
    {
        public static string NamespaceOf(this string fullyQualifiedName)
        {
            const char LastPeriod = '.';
            int namespaceIndex = fullyQualifiedName.LastIndexOf(LastPeriod);

            if (namespaceIndex > 0)
            {
                return fullyQualifiedName.Substring(0, namespaceIndex);
            }

            return string.Empty;
        }

        public static string NameWithoutNamespace(this string fullyQualifiedName)
        {
            const char LastPeriod = '.';
            int namespaceIndex = fullyQualifiedName.LastIndexOf(LastPeriod);

            if (namespaceIndex > 0)
            {
                return fullyQualifiedName.Substring(namespaceIndex + 1);
            }

            return fullyQualifiedName;
        }

        public static string FullyQualifyName(this string typeName, string namespaceName)
        {
            if (string.IsNullOrEmpty(namespaceName))
                return typeName;

            return string.Format("{0}.{1}", namespaceName, typeName);
        }

        public static string CleanTypeName(this string fullyQualifiedName)
        {
            const string Ampersand = "&";
            return fullyQualifiedName.Replace(Ampersand, string.Empty);
        }

        public static string HtmlSafeTypeName(this string fullyQualifiedName)
        {
            const string xmlLessThan = "<";
            const string htmlLessThan = "&lt;";
            const string xmlGreaterThan = ">";
            const string htmlGreaterThan = "&gt;";

            return fullyQualifiedName
                .Replace(xmlLessThan, htmlLessThan)
                .Replace(xmlGreaterThan, htmlGreaterThan);
        }

    }
}
